<style>
    .layui-badge-rim {
        height: 20px;
        border-width: 1px;
        border-style: solid;
        background-color: #eef4f5;
        font: 14px Helvetica Neue, Helvetica, PingFang SC, Tahoma, Arial, sans-serif;
        color: #6e667d;
    }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">状态：</label>
                                    <div class="layui-input-inline">
                                        <select name="status" lay-filter="status"
                                                class="common-select-search">
                                            <option value="">请选择</option>
                                            <option value="0">正常</option>
                                            <option value="1">作废</option>
                                        </select>
                                    </div>
                                </div>
                                <!-- 是否默认 -->
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">是否默认：</label>
                                    <div class="layui-input-inline">
                                        <select name="isDefault" lay-filter="isDefault"
                                                class="common-select-search">
                                            <option value="">请选择</option>
                                            <option value="1">是</option>
                                            <option value="0">否</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">BOM版本编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" id="code" class="layui-input"
                                               type="text"
                                               placeholder="请输入版本编号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">BOM版本名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" id="name" class="layui-input"
                                               type="text"
                                               placeholder="请输入版本名称" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">物资SKU料号：</label>
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input" id="skuCode" name="skuCode"
                                               placeholder="物资SKU料号">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">所属SKU编号：</label>
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input" id="skuNo" name="skuNo"
                                               placeholder="所属SKU编号">
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>
                                        重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="productDrawInfo" lay-filter="productDrawInfo"></table>
        </div>
    </div>
</div>

<script type="text/html" id="viewAttachmentHtml">
    <div id="example1" style="height: 100%;"></div>
</script>


<!-- 表格操作列 -->
<script type="text/html" id="productDrawTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
    {{# if(d.isDeleted==false){  }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancel">作废</a>
    {{# } }}
    {{# if(d.isDeleted==true){  }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="recover">恢复</a>
    {{# } }}
    {{# if(d.isDefault=='0'){  }}
    <a class="layui-btn layui-btn-xs" lay-event="default">设为默认</a>
    {{# } }}
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'laydate', 'admin', 'laytpl', 'tableTreeDj', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var config = layui.config;
        var admin = layui.admin;


        //渲染下拉
        form.render('select');

        // 渲染表格
        var insTb = table.render({
            id: '#productDrawInfo',
            elem: '#productDrawInfo',
            url: config.base + 'enterprise/res/admin/proMoldDraw/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', fixed: 'center'}
                , {field: 'code', title: '模具图纸编号', fixed: 'center',width: 160}
                , {field: 'versionName', title: '模具图纸版本名称', fixed: 'center',width: 160}
                , {field: 'drawCode', title: '产品图纸编号', fixed: 'center'}
                , {field: 'name', title: '模具图纸文件',
                    templet: function (d) {
                        return '<span class="layui-badge-rim">' + d.name + '</span>';
                    },
                    event: 'imgClick'}
                , {field: 'skuCode', title: '产品SKU料号',fixed: 'center'}
                , {field: 'assemblyNatureName', title: '产品性质',fixed: 'center'}
                , {field: 'drawTypeName', title: '图纸类别',fixed: 'center'}
                , {field: 'isDeleted',fixed: 'center', title: '状态',templet: d => d.isDeleted ? '作废' : '正常', width: 80}
                , {field: 'isDefault',fixed: 'center',templet: d => d.isDefault === '1' ? '是' : '否', title: '是否默认版本'}
                // , {field: 'createTime',fixed: 'center', title: '创建时间', width: 160}
                // , {field: 'creatorName',fixed: 'center', title: '创建人'}
                , {align: 'center', toolbar: '#productDrawTableBar', title: '操作', width: 220}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                console.log("menuButtton", menuButton);
                if (menuButton.indexOf("搜索") == -1) {
                    $('.layui-form.toolbar').remove();
                }
                if (menuButton.indexOf("修改") == -1) {
                    $("[lay-event='edit']").remove();
                }
                if (menuButton.indexOf("删除") == -1) {
                    $("[lay-event='del']").remove();
                }
            }
        });

        //设置为默认
        function doDefault(data){
            layer.confirm('确认设为默认吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/res/admin/proMoldDraw/setDefault/'+data.id,null, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload('productDrawInfo');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'PUT');
            });
        }

        // 工具条点击事件
        table.on('tool(productDrawInfo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'default') { //修改
                doDefault(data);
            }
            else if (obj.event === 'imgClick') {
                let json = {};
                json.attachment = data.fileUrl;
                showViewAttachmentModel(json);
            }
            else if (obj.event === 'detail') { //详情
                showDetailModel(data);
            } else if (obj.event === 'recover') { //恢复
                doRecover(data);
            } else if (obj.event === 'cancel') { //作废
                doCancel(data);
            }
        });

        //恢复
        function doRecover(data){
            layer.confirm('确定要恢复吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/res/admin/proMoldDraw/recover/'+data.id,null, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload('productDrawInfo');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'POST');
            });
        }

        //作废
        function doCancel(data){
            layer.confirm('确定要作废吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/res/admin/proMoldDraw/cancellation/'+data.id,null, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload('productDrawInfo');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'POST');
            });
        }

        function showViewAttachmentModel(data) {
            admin.open({
                type: 1,
                area: ['60%', '80%'],
                offset: '65px',
                title: '查看附件',
                content: $('#viewAttachmentHtml').html(),
                success: function () {
                    if (data.attachment && data.attachment.indexOf(".pdf") !== -1) {
                        PDFObject.embed(data.attachment, "#example1");
                    } else {
                        var attachmentHtml = "";
                        data.attachment.split(",").forEach((attachmentUrl, index) => {
                            attachmentHtml += '<div><div><img src="' + attachmentUrl + '"></div></div>';
                        });
                        $('#example1').html(attachmentHtml);
                    }
                }
            })
        }

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        //显示详情
        function showDetailModel(data) {
            admin.putTempData("drawData",data);
            let title = "详情";
            let path = "components/res/draw/proDrawMoldDetailFrom.html";
            admin.formOpen({
                type: 1,
                area: ['1580px', '75%'],
                style: 'font-size: 20px;',
                title: title,
                path: path,
                finish: function () {
                    table.reload('productDrawInfo', {});
                }
            });
        }
    });
</script>